package com.material.c2c.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.log4j.LogManager;
//import org.apache.log4j.Logger;


public class ListUtil {
	
//	private static final Logger logger = LogManager.getLogger(ListUtil.class);
	
	/**
	 * 匹配数组 计算需要添加或删除的值
	 * @author: wt
	 * @param dbList	//数据库数据
	 * @param newList	//新数据
	 * @return map.get("add");//新增 map.get("del");//删除
	 */
	public static Map<String, List<String>> matchAddDeleteList(List<String> dbList, List<String> newList){
		Map<String, List<String>> resultMap = new HashMap<String, List<String>>();
		List<String> addList = null;//新增
		List<String> delList = null;//删除
		List<String> nochangeList = null;//未修改
		if (dbList == null || dbList.size() == 0) {
			//全部为新增
			addList = newList;
		}else{
			if (newList != null && newList.size() > 0){
				addList = new ArrayList<String>();//新增 初始化
				delList = new ArrayList<String>();//删除 初始化
				nochangeList = new ArrayList<String>();//未修改 初始化
				//部分 新增 删除
				for (String newStr : newList) {
					if (dbList.contains(newStr)) {
						nochangeList.add(newStr);
						dbList.remove(newStr);//删除
					}else {
						addList.add(newStr);
					}
				}
				//其他数据为删除
				delList = dbList;
			}else {
				//全部为删除
				delList = dbList;
			}
		}
		resultMap.put("add", addList);
		resultMap.put("del", delList);
		resultMap.put("update", nochangeList);
		// logger.debug("match->add:"+(addList!=null?addList.size():"null")+",del:"+(delList!=null?delList.size():"null")+",nochange:"+(nochangeList!=null?nochangeList.size():"null")+",data:"+JsonUtil.toJson(resultMap));
		return resultMap;
	}
	
	public static void main(String[] args) {
		List<String> dbList = new ArrayList<String>();
		List<String> newList = new ArrayList<String>();
		dbList.add("1");
		dbList.add("2");
		dbList.add("3");
		newList.add("2");
		newList.add("3");
		newList.add("4");
		newList.add("5");
		Map<String, List<String>> matchMap = ListUtil.matchAddDeleteList(dbList, newList);
		List<String> addList = matchMap.get("add");
		List<String> delList = matchMap.get("del");
		// logger.info(JsonUtil.toJson(addList));
		// logger.info(JsonUtil.toJson(delList));
		
	}
}
